package cn.ffcs.sp.web;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;


import com.ffcs.crm.intf.webServiceClient.forPoc.PocHandleTest;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.security.model.User;
import cn.ffcs.security.web.UserContext;

/**
 * 页面逻辑控制层基础代码<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.huangjx
 * @version $Id$
 */
public class SPManagerAction extends BaseAction
{
    /**
     * 转跳页面
     * @return
     */ 
    String act;
    /**
     * SP详情URL
     * @return
     */ 
    String url;
    

    /**
     * 用户
     * @return
     */ 
    User user;

    /**
     * 分页信息
     * @return
     */ 
    private String pageInfo="1,10,1";//总数+一页数+当前页
    private int[] str={1,10,1};
    /**
     * 获取合作商基础信息
     * @return
     */ 
    public String toSp(){
        return act;
    }
    
    public String findBaseInfo()
    {
        try{
           // str=pageInfo.split(",");
            ActionContext ctx = ActionContext.getContext();  
            HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
            HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
            String ifUser="POC";//此标识用于对发起调用系统进行验证和授权，统一指定为“POC”，此标识确定后不会改变。
            String ifPass="POC321";//此校验码用于对发起调用系统进行验证，此校验码改变时需要在双方系统同时改变，目前指定为” POC321”。
            String ifCode="";
            String ifEvent="";
            String info="";//对于传送信息类的接口，info中主要包括传送的信息对于查询信息类的接口，info中主要是查询条件
            String qryType=request.getParameter("qryType");//spinfo:sp信息查询spmonthin:月结查询 spct:客户投诉查询
            StringBuffer infoStr=new StringBuffer("<?xml  version=\"1.0\" encoding=\"GB2312\"?>\n");
            infoStr.append("<ifInfo>\n");
            infoStr.append("<qryType>spinfo</qryType>\n");//spinfo:sp信息查询spmonthin:月结查询 spct:客户投诉查询
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF1</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF1")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>接入平台</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF2</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF2")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>服务类型</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF3</fieldName>\n");
            String INF3="";
            if(request.getParameter("INF3")!=null){
                INF3=new String(request.getParameter("INF3").getBytes("ISO-8859-1"),"utf-8");
            }
            infoStr.append("<fieldValue>"+INF3+"</fieldValue>\n");
            infoStr.append("<other>like</other>\n");
            infoStr.append("<comment>SP/AP名称</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF4</fieldName>\n");
            String INF4="";
            if(request.getParameter("INF4")!=null){
                INF4=new String(request.getParameter("INF4").getBytes("ISO-8859-1"),"utf-8");
            }
            infoStr.append("<fieldValue>"+INF4+"</fieldValue>\n");
            infoStr.append("<other>like</other>\n");
            infoStr.append("<comment>SP/AP地址</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF5</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF5")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>SP/AP代码</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF6</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF6")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>管理部门</comment>\n");
            infoStr.append("</params>\n");
            if(pageControl==null){
                pageControl=new PageControl();
            }
            infoStr.append("<num Note='请求数量'>"+str[1]+"</num>\n");
            infoStr.append("<start Note='请求数据开始'>"+str[2]+"</start>\n");
            infoStr.append("<order Note='数据排序'></order>\n");
            infoStr.append("</ifInfo>");
            info=infoStr.toString();
            PocHandleTest pocHandleTest=new PocHandleTest();
//            System.out.println("info=\n"+info);
            String xml =pocHandleTest.fjpocDataService(ifUser, ifPass, ifCode, ifEvent, info);
//            System.out.println("backxml=\n"+xml);
            //String xml=fjpocDataService(ifUser, ifPass, ifCode, ifEvent, info);
            xml=xml.replaceAll("UTF-8", "GB2312");
            if(xml.indexOf("<count>")!=-1){
                String count=xml.substring(xml.indexOf("<count>")+7,xml.indexOf("</count>"));
                xml=xml.replaceAll("<count>"+count+"</count>", "<pageInfo>"+count+","+str[1]+","+str[2]+"</pageInfo>");
            }
//            System.out.println("xml=\n"+xml);
            response.setContentType("text/xml;charset=gb2312");
            response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取合作商结算信息
     * @return
     */  
    public String findBalance()
    {
        try{
           // str=pageInfo.split(",");
            ActionContext ctx = ActionContext.getContext();  
            HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
            HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
            String ifUser="POC";//此标识用于对发起调用系统进行验证和授权，统一指定为“POC”，此标识确定后不会改变。
            String ifPass="POC321";//此校验码用于对发起调用系统进行验证，此校验码改变时需要在双方系统同时改变，目前指定为” POC321”。
            String ifCode="";
            String ifEvent="";
            String info="";//对于传送信息类的接口，info中主要包括传送的信息对于查询信息类的接口，info中主要是查询条件
            String time="";
            //request.getParameter("year")+request.getParameter("month")
            if(request.getParameter("year")!=null){
                time=request.getParameter("year");
            }
            if(request.getParameter("month")!=null&&!request.getParameter("month").equals("")){
                time+="-"+request.getParameter("month");
            }
            StringBuffer infoStr=new StringBuffer("<?xml  version=\"1.0\" encoding=\"GB2312\"?>\n");
            infoStr.append("<ifInfo>\n");
            infoStr.append("<qryType>spmonthin</qryType>\n");//spinfo:sp信息查询spmonthin:月结查询 spct:客户投诉查询
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF1</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF1")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>接入平台</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF2</fieldName>\n");
            String INF2="";
            if(request.getParameter("INF2")!=null){
                INF2=new String(request.getParameter("INF2").getBytes("ISO-8859-1"),"utf-8");
            }
            infoStr.append("<fieldValue>"+INF2+"</fieldValue>\n");
            infoStr.append("<other>like</other>\n");
            infoStr.append("<comment>SP/AP名</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF3</fieldName>\n");
            infoStr.append("<fieldValue>"+time+"</fieldValue>\n");
            infoStr.append("<other><![CDATA["+request.getParameter("compare")+"]]></other>\n");
            infoStr.append("<comment>月结年月</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF4</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF4")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>状态</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF5</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF5")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>模板分类</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF6</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF6")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>SP编码</comment>\n");
            infoStr.append("</params>\n");

            if(pageControl==null){
                pageControl=new PageControl();
            }
            infoStr.append("<num Note='请求数量'>"+str[1]+"</num>\n");
            infoStr.append("<start Note='请求数据开始'>"+str[2]+"</start>\n");
            infoStr.append("<order Note='数据排序'></order>\n");
            infoStr.append("</ifInfo>");
            info=infoStr.toString();
//            System.out.println("info=\n"+info);
            PocHandleTest pocHandleTest=new PocHandleTest();
            String xml =pocHandleTest.fjpocDataService(ifUser, ifPass, ifCode, ifEvent, info);
//            System.out.println("backxml=\n"+xml);
            xml=xml.replaceAll("UTF-8", "GB2312");
            if(xml.indexOf("<count>")!=-1){
                String count=xml.substring(xml.indexOf("<count>")+7,xml.indexOf("</count>"));
                xml=xml.replaceAll("<count>"+count+"</count>", "<pageInfo>"+count+","+str[1]+","+str[2]+"</pageInfo>");
            }
//            System.out.println("xml=\n"+xml);
            response.setContentType("text/xml;charset=gb2312");
            response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取合作商投诉统计
     * @return
     */  
    public String findComplain()
    {   
        
        ActionContext ctx = ActionContext.getContext();  
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        try{
           // str=pageInfo.split(",");
            request.setCharacterEncoding("iso8859-1");
            request.setCharacterEncoding("utf-8");
            String ifUser="POC";//此标识用于对发起调用系统进行验证和授权，统一指定为“POC”，此标识确定后不会改变。
            String ifPass="POC321";//此校验码用于对发起调用系统进行验证，此校验码改变时需要在双方系统同时改变，目前指定为” POC321”。
            String ifCode="";
            String ifEvent="";
            String info="";//对于传送信息类的接口，info中主要包括传送的信息对于查询信息类的接口，info中主要是查询条件
            String time="";
            //request.getParameter("year")+request.getParameter("month")
            if(request.getParameter("year")!=null){
                time=request.getParameter("year");
            }
            if(request.getParameter("month")!=null&&!request.getParameter("month").equals("")){
                time+="-"+request.getParameter("month");
            }
            StringBuffer infoStr=new StringBuffer("<?xml  version=\"1.0\" encoding=\"GB2312\"?>\n");
            infoStr.append("<ifInfo>\n");
            infoStr.append("<qryType>spct</qryType>\n");//spinfo:sp信息查询spmonthin:月结查询 spct:客户投诉查询
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF1</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF1")+"</fieldValue>\n");
            infoStr.append("<other><![CDATA["+request.getParameter("compare")+"]]></other>\n");
            infoStr.append("<comment>投诉日期</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF2</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF2")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>主叫号码</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF3</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF3")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>服务代码</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF4</fieldName>\n");
            String INF4="";
            if(request.getParameter("INF4")!=null){
                INF4=new String(request.getParameter("INF4").getBytes("ISO-8859-1"),"utf-8");
            }
            infoStr.append("<fieldValue>"+INF4+"</fieldValue>\n");
            infoStr.append("<other>like</other>\n");
            infoStr.append("<comment>业务名称</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF5</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF5")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>投诉单号</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF6</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF6")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>障碍号码</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF7</fieldName>\n");
            infoStr.append("<fieldValue>"+request.getParameter("INF7")+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>投诉状态</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF8</fieldName>\n");
            String INF8="";
            if(request.getParameter("INF8")!=null){
                INF8=new String(request.getParameter("INF8").getBytes("ISO-8859-1"),"utf-8");
            }
            infoStr.append("<fieldValue>"+INF8+"</fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>投诉内容</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF9</fieldName>\n");
            infoStr.append("<fieldValue><![CDATA["+request.getParameter("INF9")+"]]></fieldValue>\n");
            infoStr.append("<other>=</other>\n");
            infoStr.append("<comment>接入平台</comment>\n");
            infoStr.append("</params>\n");
            
            infoStr.append("<params>\n");
            infoStr.append("<fieldName>INF10</fieldName>\n");
            String INF10="";
            if(request.getParameter("INF10")!=null){
                INF10=new String(request.getParameter("INF10").getBytes("ISO-8859-1"),"utf-8");
            }
            infoStr.append("<fieldValue>"+INF10+"</fieldValue>\n");
            infoStr.append("<other>like</other>\n");
            infoStr.append("<comment>SP名称</comment>\n");
            infoStr.append("</params>\n");

            
            if(pageControl==null){
                pageControl=new PageControl();
            }
            infoStr.append("<num Note='请求数量'>"+str[1]+"</num>\n");
            infoStr.append("<start Note='请求数据开始'>"+str[2]+"</start>\n");
            infoStr.append("<order Note='数据排序'></order>\n");
            infoStr.append("</ifInfo>");
            info=infoStr.toString();
//            System.out.println("info=\n"+info);
            PocHandleTest pocHandleTest=new PocHandleTest();
            String xml =pocHandleTest.fjpocDataService(ifUser, ifPass, ifCode, ifEvent, info);
//            System.out.println("backxml=\n"+xml);
            xml=xml.replaceAll("UTF-8", "GB2312");
            if(xml.indexOf("<count>")!=-1){
                String count=xml.substring(xml.indexOf("<count>")+7,xml.indexOf("</count>"));
                xml=xml.replaceAll("<count>"+count+"</count>", "<pageInfo>"+count+","+str[1]+","+str[2]+"</pageInfo>");
            }
//            System.out.println("xml=\n"+xml);
       
            response.setContentType("text/xml;charset=gb2312");
            response.getWriter().write(xml);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public String fjpocDataService(String ifUser,String ifPass,String ifCode,String ifEvent,String info){
        String xml="";
        StringBuffer xmlStr=new StringBuffer("<?xml  version=\"1.0\" encoding=\"GB2312\"?>\n");
        //
        xmlStr.append("<results>\n");
        xmlStr.append("<count>45</count>\n");
        xmlStr.append("<data>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF1</fileName>\n");
        xmlStr.append("<fileValue>中国电信</fileValue>\n");
        xmlStr.append("<comment>SP名称</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF2</fileName>\n");
        xmlStr.append("<fileValue>001</fileValue>\n");
        xmlStr.append("<comment>SP编码</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF3</fileName>\n");
        xmlStr.append("<fileValue>自营平台</fileValue>\n");
        xmlStr.append("<comment>接入平台</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF3</fileName>\n");
        xmlStr.append("<fileValue>服务类型1</fileValue>\n");
        xmlStr.append("<comment>服务类型</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF4</fileName>\n");
        xmlStr.append("<fileValue>管理部门1</fileValue>\n");
        xmlStr.append("<comment>管理部门</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF6</fileName>\n");
        xmlStr.append("<fileValue>状态1</fileValue>\n");
        xmlStr.append("<comment>状态</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("</data>\n");
        
        xmlStr.append("<data>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF1</fileName>\n");
        xmlStr.append("<fileValue>中国联通</fileValue>\n");
        xmlStr.append("<comment>SP名称</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF2</fileName>\n");
        xmlStr.append("<fileValue>002</fileValue>\n");
        xmlStr.append("<comment>SP编码</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF3</fileName>\n");
        xmlStr.append("<fileValue>自营平台22</fileValue>\n");
        xmlStr.append("<comment>接入平台</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF3</fileName>\n");
        xmlStr.append("<fileValue>服务类型2</fileValue>\n");
        xmlStr.append("<comment>服务类型</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF4</fileName>\n");
        xmlStr.append("<fileValue>管理部门2</fileValue>\n");
        xmlStr.append("<comment>管理部门</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("<field>\n");
        xmlStr.append("<fileName>OUTF6</fileName>\n");
        xmlStr.append("<fileValue>状态2</fileValue>\n");
        xmlStr.append("<comment>状态</comment>\n");
        xmlStr.append("</field>\n");
        
        xmlStr.append("</data>\n");
        xmlStr.append("</results>");

        return xmlStr.toString();
    }
    public String showDetail(){
        ActionContext ctx = ActionContext.getContext();  
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        user = ((UserContext) request.getSession().getAttribute("LOGINUSER_CONTEXT")).getCurrUser();
        Properties prop = new Properties();
        InputStream in = null;
        try {
            in = this.getClass().getResourceAsStream(
                    "/sp-server.properties");
            prop.load(in);
            url = prop.getProperty("sp.server.url");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
        }
        if(act.equals("toBaseInfo")){
            url = url.substring(0, url.indexOf("/sp/") + 4)
                + "module_share/spmsQrySpInfo.htm";
        }else{
            url=url.substring(0,url.indexOf("/sp/")+4)+"module_share/spmsQryCtInfo.htm";
        }
        return act;
    }
    public String getAct()
    {
        return act;
    }
    public void setAct(String act)
    {
        this.act = act;
    }
    public String getPageInfo()
    {
        return pageInfo;
    }

    public void setPageInfo(String pageInfo)
    {
        this.pageInfo = pageInfo;
    }

    public int[] getStr()
    {
        return str;
    }

    public void setStr(int[] str)
    {
        this.str = str;
    }
    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }
    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }
}
